package org.zjx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.zjx.common.util.ImageUtil;
import org.zjx.dao.entity.Prop;
import org.zjx.dao.mapper.PropMapper;
import org.zjx.service.IPropService;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 
 * @since 2025-05-28
 */
@Service
public class PropServiceImpl extends ServiceImpl<PropMapper, Prop> implements IPropService {

    @Override
    public List<Prop> identifyProp(List<String> propStrings) throws IOException {

        if (CollectionUtils.isEmpty(propStrings)) {
            return Collections.emptyList();
        }

        List<Prop> dataProps = this.list();
        List<Prop> result = new ArrayList<>();

        for (String propString : propStrings) {
            if (propString.isEmpty()){
                result.add(null);
                continue;
            }
            boolean isOK = false;
            BufferedImage bufferedImage = ImageUtil.stringToBufferedImage(propString);
            for (Prop prop : dataProps) {
                BufferedImage dataBufferImage = ImageUtil.stringToBufferedImage(prop.getImageString());
                List<Point> points = ImageUtil.matchTemplate(bufferedImage,dataBufferImage);
                if (!points.isEmpty()) {
                    result.add(prop);
                    isOK = true;
                    break;
                }
            }
            if (!isOK) {
                result.add(null);
            }
        }

        // 执行查询
        return result;
    }
}
